<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 店铺信息
 * @author khj
 */
class Store extends MY_Controller {
    /**
     * @var MerchantMessageModel
     */
    public $mMessage = null;

	public function __construct(){
		parent::__construct();
	}

    /**
     * 店铺信息
     */
    public function storeInfo(){
        $this->loadModel('AdmSettingModel', 'mAdm', 'admin');

        $info = $this->mUser->getShopInfo($this->userId,true, true);

        //周营业日
        $weekarray=array("","周一","周二","周三","周四","周五","周六","周日"); //定义一个数组
        $wk = '';
        if($info['profile']['business_week']){
            $wk .= '(';
            $week = array_filter(explode(',',$info['profile']['business_week']));
            foreach ($week as $k=>$v){
                $wk .= $weekarray[$v];
                if($k != count($week)){
                    $wk .= '、';
                }
            }
            $wk .= ')';
        }

        //区域名称

        $locationId = array($info['merchant']['province'],$info['merchant']['city'],$info['merchant']['county']);

        $location = $this->mAdm->getLocationNameByIds($locationId);

        $area = $location[$info['merchant']['province']]['name'].$location[$info['merchant']['city']]['name'].$location[$info['merchant']['county']]['name'];

        $data = array(
            'shop_name' => $info['merchant']['shop_name'],
            'auto_take' => $info['merchant']['auto_take'],
            'min_consume' => $info['merchant']['min_consume'],
            'portrait' => $info['merchant']['portrait'],
            'area' => $area,
            'address' => $info['merchant']['address'],
            'video' => $info['merchant']['shop_video'],
            'business_time' => $info['profile']['business_time'],
            'business_week' => $wk,
            'business' => $info['profile']['business']?$info['profile']['business']:'',
            'store_tel' => $info['profile']['mobile'],
            'shop_imgs' => $info['profile']['shop_imgs'],
            'shop_ads' => $info['profile']['shop_ads'],
            'shipping_type' => $info['profile']['shipping_type'],
            'expect_arrive_time' => $info['profile']['expect_arrive_time'],
        );

        $this->appReturn(1, '', $data);
    }

    /**
     * 店铺其他设置
     */
    public function setStore(){

        //表单验证
        $vConf = array(
            array('keyword', '关键字', 'required'),
            array('value', '设置内容', 'required'),
        );

        $keyword = trim(post('keyword'));
        $value = post('value');

        if(!in_array($keyword,array('store_tel','expect_arrive_time','shipping_type','min_consume','auto_take'))){
            $this->appReturn(0, '关键字错误');
        }

        if($keyword == 'expect_arrive_time'){//送达时间
            if(!is_numeric($value) || $value > 60){
                $this->appReturn(0, '送达时间必须是数字且小于一个小时');
            }
        }
        if($keyword == 'shipping_type' && !in_array($value, array(1,2))){//配送方式
            $this->appReturn(0, '配送方式错误');
        }
        if($keyword == 'min_consume' && !is_numeric($value)){//最低消费
            $this->appReturn(0, '最低消费必须是数字');
        }
        if($keyword == 'auto_take' && !in_array($value, array(1,2))){//接单方式
            $this->appReturn(0, '接单方式错误');
        }
        //保存信息
        if(!($this->mUser->save(array($keyword => $value), $this->userId))){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     * 设置店铺营业时间
     */
    public function setBusinessTime(){

        //表单验证
        $vConf = array(
            array('time', '营业时间', 'required')
        );
        $this->formDataValidation($vConf);

        $business = trim(post('time'));

        $dbT = $this->mUser->deleteBusinessTime($this->userId);
        if (!$dbT){
            return array('status' => 0,'message' => '保存失败');
        }
        $data = array();
        $b = array();

        $business = json_decode($business, true);
        foreach ($business as $k => $v){
            if(!is_int(strtotime($v['start'])) || !is_int(strtotime($v['end']))){
                $this->appReturn(0, '时间格式错误');
            }
            $start = time_to_second($v['start']);
            $end = time_to_second($v['end']);
            if ($end <= $start) {
                $this->appReturn(0, '结束时间必须大于开始时间');
            }
            if ($k > 0) {
                foreach ($b as $vv) {
                    $bus = explode('|', $vv);
                    if ($bus[0] < $start && $start < $bus[1]) {
                        $this->appReturn(0, '时间段存在交集');
                    }
                    if ($bus[0] < $end && $end < $bus[1]) {
                        $this->appReturn(0, '时间段存在交集');
                    }
                }
            }
            $b[$k] = $start . '|' . $end;
            $data[$k] = array(
                'merchant_id' => $this->userId,
                'business_time_start' => $start,
                'business_time_end' => $end,
            );

        }
        unset($b,$business);

        $tA = $this->mUser->saveBusinessTime($data);

        if(!$tA){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     * PHP计算两个时间段是否有交集（边界重叠不算）
     *
     * @param string $beginTime1 开始时间1
     * @param string $endTime1 结束时间1
     * @param string $beginTime2 开始时间2
     * @param string $endTime2 结束时间2
     * @return bool
     */
    public  function is_time_cross($beginTime1 = '', $endTime1 = '', $beginTime2 = '', $endTime2 = '') {
        $status = $beginTime2 - $beginTime1;

        if ($status > 0) {
            $status2 = $beginTime2 - $endTime1;
            if ($status2 >= 0) {
                return false;
            } else {
                return true;
            }
        } else {
            $status2 = $endTime2 - $beginTime1;

            if ($status2 > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    /**
     * 设置店铺周营业日
     */
    public function setBusinessWeek(){

        //表单验证
        $vConf = array(
            array('business_week', '周营业日', 'required')
        );
        $this->formDataValidation($vConf);

        $business_week = trim(post('business_week'));

        $week = json_decode($business_week, true);
        $weekArray = array(1,2,3,4,5,6,7);
        foreach ($week as $v){
            if(!in_array($v,$weekArray)){
                $this->appReturn(0, '周营业日错误');
            }
        }

        sort($week);//排序

        //保存信息
        $data = array(
            'business_week' => ','.implode(',',$week).','
        );


        $re = $this->mUser->save($data, $this->userId);
        if(!$re){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     *设置提现密码
     */
    public function setWithdrawPassword(){
        //表单验证
        $vConf = array(
            array('code', '验证码', 'required'),
            array('password', '密码', 'required|min_length[6]|max_length[15]'),
            array('re_password', '确认密码', 'required|matches[password]'),
        );
        $this->formDataValidation($vConf);
        $mobile = $this->userMobile;
        $code = trim(post('code'));

        //验证验证码
        $tA = $this->mUser->getSmsCodeLogInfo($mobile, $code);
        if(empty($tA)) $this->appReturn(0, '验证码错误');
//        $smsExpire = time() - getCustomConfig('sms_expire') * 3600;
//        if($tA['addtime'] < $smsExpire){
//            $this->appReturn(0, '验证码过期');
//        }

        //验证手机
        $userId = $this->mUser->mobileIsExist($mobile);
        if( ! $userId){
            $this->appReturn(0, '手机号码未注册');
        }

        $data = array(
            'withdraw_password' => setPassword(post('password'))
        );

        if($this->mUser->save($data, $userId)){
            $this->mUser->deleteSmsCode($mobile);//删除验证码
            $this->appReturn(1, '保存成功');
        }

        $this->appReturn(0, '保存失败');
    }

    /**
     * 修改提现密码
     */
    public function withdrawPassword(){
        //表单验证
        $vConf = array(
            array('old_password', '旧密码', 'required|min_length[6]|max_length[15]'),
            array('password', '新密码', 'required|min_length[6]|max_length[15]'),
            array('re_password', '确认密码', 'required|matches[password]'),
        );
        $this->formDataValidation($vConf);
        $mobile = $this->userMobile;
        $oldPassword = setPassword(post('old_password'));

        //验证
        $userId = $this->mUser->getInfoByIdAndWithdrawPassword($this->userId, $oldPassword);
        if(empty($userId)){
            $this->appReturn(0, '原密码错误');
        }

        $data = array(
            'withdraw_password' => setPassword(post('password'))
        );

        if($this->mUser->save($data, $this->userId)){
            $this->appReturn(1, '修改成功');
        }
        $this->appReturn(0, '修改失败');
    }

    /**
     *意见反馈
     */
    public function feedback(){
        $this->loadModel('AdmFeedbackModel', 'mFeed', 'admin');

        //表单验证
        $vConf = array(
            array('content', '反馈内容', 'required'),
        );
        $this->formDataValidation($vConf);

        $data = array(
            'content' => trim(post('content')),
            'merchant_id' => $this->userId,
            'status' => 1,
            'addtime' => time(),
        );

        //保存信息
        if(!($this->mFeed->save($data))){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     * 历史反馈
     */
    public function feedbackList(){
        $this->loadModel('AdmFeedbackModel', 'mFeed', 'admin');
        //表单验证
        $vConf = array(
            array('page', '页码', 'required|is_natural_no_zero'),

        );
        $this->formDataValidation($vConf);

        $page = (int)post('page');


        $tA = $this->mFeed->appGetFeedbackList(0, $this->userId, $page);

        $this->appReturn(1,'', $tA);
    }

    /**
     * 图片上传临时文件
     */
    public function imgsTemp(){

        $path = 'uploads/temp';
        $img = array();

        if(!empty($_FILES)){
            foreach ($_FILES as $k=>$v){
                $tS = imageUpload($path, $k, date('ymdhis').rand(1000, 100000),true);
                if( ! $tS['status']){
                    $this->appReturn(0,'上传失败');
                }
                $img[] = base_url($tS['data']);
            }
        }

        //是否存在临时图片地址
        foreach ($_POST as $key=>$value){
            if(stripos($key,'img') !== FALSE){
                $img[] = $value;
            }
        }


        $this->appReturn(1, '', $img);
    }

    /**
     * 上传店铺图片
     */
    public function addStoreImg(){

        //表单验证
        $vConf = array(
            array('images', '图片', 'required')
        );
        $this->formDataValidation($vConf);

        $images = json_decode(stripslashes(trim(post('images'))));//上传的图片json

        if( ! is_array($images)){
           $this->appReturn(0, '图片格式错误');
        }

        //上传图片
        $imgPath = array();
        $path = 'uploads/'.$this->port.'/'.$this->userId.'/store';
        foreach ($images as $k => $v){

        	if (stripos($v,'temp') !== FALSE){

        		$res = saveTempImg($v, $path, true);

        		if ($res){
        			$imgPath[] = $res;
        		}
        	}
            if (stripos($v,'uploads/merchant') !== FALSE){
                $imgPath[] =  str_replace(base_url(), '', $v);

            }
        }

        //获取店铺信息
        $info = $this->mUser->getInfo($this->userId, 0, true);

        if( isset($info['shop_imgs']) && $info['shop_imgs'] ){

            $oldImg = $info['shop_imgs'];

            foreach ($oldImg as $key=>$v){

            	if( ! in_array($v, $imgPath)){
            		
            		if (stripos($v, '?') !== false){
            			//查找?存在处理
            			$v = strstr($v, '?',true);
            		}
            		
                    $tA = explode('/', $v);
                    $filename = $tA[count($tA)-1];
                    if (file_exists($v)){
                        unlink($v);
                    }
                   
                    if (file_exists($path.'sm/'.$filename)){
                        unlink($path.'sm/'.$filename);
                    }
                   
                    if (file_exists($path.'xs/'.$filename)){
                        unlink($path.'xs/'.$filename);
                    }
                  
                }
            }
        }

        //保存信息

        if($imgPath){
            $imgPath = json_encode($imgPath);
        }else{
            $imgPath = '';
        }
        if(!($this->mUser->save(array('shop_imgs' => $imgPath), $this->userId))){
            $this->appReturn(0, '保存失败');
        }
        $this->appReturn(1,'保存成功',$imgPath);
    }

    /**
     * 上传店铺广告
     */
    public function addStoreAds(){

        //表单验证
        $vConf = array(
            array('images', '图片', 'required')
        );
        $this->formDataValidation($vConf);

        $images = json_decode(stripslashes(trim(post('images'))));//上传的图片json

        if( ! is_array($images)){
            $this->appReturn(0, '图片格式错误');
        }

        //上传图片
        $imgPath = array();
        $imgData = array();
        $path = 'uploads/'.$this->port.'/'.$this->userId.'/storeads';
        foreach ($images as $k => $v){

            if (stripos($v,'temp') !== FALSE){

                $res = saveTempImg($v, $path, true);

                if ($res){
                    $imgPath[] = ['id' => '','productName' => '', 'adpic' => $res];
                    $imgData[] = $res;
                }
            }
            if (stripos($v,'uploads/merchant') !== FALSE){
                $imgPath[] = ['id' => '','productName' => '', 'adpic' => str_replace(base_url(), '', $v)];
                $imgData[] = str_replace(base_url(), '', $v);
            }
        }
        $this->loadModel('ConsumerProductModel', 'mProduct', 'consumer');
        //获取店铺信息
        $info = $this->mUser->getInfo($this->userId, 0, true);

        if( isset($info['shop_ads']) && $info['shop_ads'] ){

            $oldImg = $info['shop_ads'];
            $ids = [];
            foreach ($oldImg as $key=>$value){
                if($value['id'] && !in_array($value['id'], $ids)){
                    $ids[] = $value['id'];
                }
            }
            //查询商品名称
            $products = $this->mProduct->getProductByIds($ids);

            foreach ($oldImg as $key=>$v){

                if( ! in_array($v['adpic'], $imgData)){

                    if (stripos($v['adpic'], '?') !== false){
                        //查找?存在处理
                        $v['adpic'] = strstr($v['adpic'], '?',true);
                    }

                    $tA = explode('/', $v['adpic']);
                    $filename = $tA[count($tA)-1];
                    if (file_exists($v['adpic'])){
                        unlink($v['adpic']);
                    }

                    if (file_exists($path.'sm/'.$filename)){
                        unlink($path.'sm/'.$filename);
                    }

                    if (file_exists($path.'xs/'.$filename)){
                        unlink($path.'xs/'.$filename);
                    }
                }else{

                    foreach ($imgPath as $ky => $img){
                        if($img['adpic'] == $v['adpic']){
                            $imgPath[$ky]['id'] = $v['id'];
                            $imgPath[$ky]['productName'] = isset($products[$v['id']]) ? $products[$v['id']]['name']:'';
                        }
                    }
                }
            }
        }

        //保存信息

        if($imgPath){
            $imgPath = json_encode($imgPath, JSON_UNESCAPED_UNICODE);
        }else{
            $imgPath = '';
        }

        if(!($this->mUser->save(array('shop_ads' => $imgPath), $this->userId))){
            $this->appReturn(0, '保存失败');
        }
        $this->appReturn(1,'保存成功',$imgPath);
    }
    /**
     * 上传视频（只有高级商户rank = 3 才能上传视频）
     */
    public function addVideo(){

        //获取用户信息
        $info = $this->mUser->getInfo($this->userId, 0, true);
        if($info['rank'] != 3){
            $this->appReturn(0, '只有高级商户才能上传视频');
        }

        $path = 'uploads/'.$this->port.'/'.$this->userId.'/video';

        $data = fileUpload($path, 'video', 'video');

        if($data['status']){
            //保存视频
            if(!($this->mUser->save(array('shop_video' => $data['data']), $this->userId))){
                $this->appReturn(0, '保存失败');
            }
            $this->appReturn(1, '保存成功', base_url($data['data']));
        }

        $this->appReturn(0, '上传失败');
    }

    /**
     * 可申请的广告位
     * 1首页   6商盟顶部轮播 7 商盟中部左边 8商盟中部右上 9商盟中部右下
     */
    public function adsPosition(){
        $this->loadModel('MerchantAdsModel', 'mAds', 'merchant');

        $data = $this->mAds->adsPosition();

        $this->appReturn(1, '', $data);
    }

    /**
     * 广告位申请
     */
    public function applyAds(){
        $this->loadModel('MerchantAdsModel', 'mAds', 'merchant');
        //表单验证
        $vConf = array(
            array('ads_position', '广告位置', 'required|in_list[1,6,7,8,9]'),
            array('day', '周期（天）', 'required|is_natural_no_zero'),
            array('content', '其他需求', 'required'),
        );
        $this->formDataValidation($vConf);

        $data = array(
            'merchant_id' => $this->userId,
            'ad_position' => post('ads_position'),
            'day' => post('day'),
            'content' => trim(post('content')),
            'addtime' => time(),
            'status' => 1,
            'is_delete' => 1,
        );
        //保存信息
        if(!($this->mAds->save($data))){
            $this->appReturn(0, '提交失败');
        }
        $this->appReturn(1, '提交成功');
    }

    /**
     * 广告申请记录
     */
    public function getApplyAdsList(){
        $this->loadModel('MerchantAdsModel', 'mAds', 'merchant');
        //表单验证
        $vConf = array(
            array('page', '页码', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);
        $page = (int)post('page');

        $data = $this->mAds->appGetApplyList($this->userId, $page);

        $this->appReturn(1, '', $data);
    }

    /**
     * 取消广告申请
     */
    public function cancelApplyAds(){
        $this->loadModel('MerchantAdsModel', 'mAds', 'merchant');
        //表单验证
        $vConf = array(
            array('id', '申请记录id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);
        $id = (int)post('id');

        //验证记录是否存在
        $info = $this->mAds->getInfo($id, $this->userId);
        if(!$info){
            $this->appReturn(0, '记录不存在');
        }

        if($info['status'] != 1){
            $this->appReturn(0, '仅申请中的记录可取消');
        }

        //取消申请
        if(!($this->mAds->save(array('status' => 5), $id))){
            $this->appReturn(0, '取消失败');
        }

        $this->appReturn(1, '取消成功');
    }
    /**
     * 删除广告申请记录
     */
    public function delApplyAds(){
        $this->loadModel('MerchantAdsModel', 'mAds', 'merchant');
        //表单验证
        $vConf = array(
            array('id', '申请记录id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);
        $id = (int)post('id');

        //验证记录是否存在
        $info = $this->mAds->getInfo($id, $this->userId);
        if(!$info){
            $this->appReturn(0, '记录不存在');
        }

        if(!in_array($info['status'], array(3,4,5))){
            $this->appReturn(0, '记录不可删除');
        }
        //删除
        if($info['status'] == 4 || $info['status'] == 5){
            if(!($query = $this->mAds->deleteAds($id, $this->userId))){
                $this->appReturn(0, '删除失败');
            }
        }
        //假删
        if($info['status'] == 3){
            if(!($query = $this->mAds->save(array('is_delete' => 2),$id))){
                $this->appReturn(0, '删除失败');
            }
        }
        $this->appReturn(1, '删除成功');
    }

    /**
     * 店铺评价
     */
    public function comment(){
        $this->loadModel('MerchantCommentModel', 'mComment', 'merchant');
        $this->loadModel('AdmSettingModel', 'mSetting', 'admin');
        $info = $this->mComment->getStoreCommentScore($this->userId);
        $info['avg'] = $info['avg'] ? sprintf('%.1f', $info['avg']) : '5.0';
        $info['service_avg'] = $info['service_avg'] ? sprintf('%.1f', $info['service_avg']) : '5.0';
        //店铺标签
        $tags = $this->mSetting->get('merchant_comment_tags');
        $tags = json_decode($tags);
        $tagsData = array();
        foreach ($tags as $k=>$v){
            $count = $this->mComment->getCommentTagsNumber($this->userId, $v);
            $tagsData[] = array('tags' => $v,'count' => $count);
        }
        $info['tags'] = $tagsData;
        $this->appReturn(1, '', $info);
    }

    /**
     * 店铺全部评论
     * type 类型 1全部 2好评 3差评
     */
    public function getCommentList(){
        $this->loadModel('MerchantCommentModel', 'mComment', 'merchant');

        //表单验证
        $vConf = array(
            array('page', '页码', 'required|is_natural_no_zero'),
            array('type', '类型', 'required|is_numeric')
        );
        $this->formDataValidation($vConf);
        $page = (int)post('page');
        $type = (int)post('type');
        //获取评论列表
        $data = $this->mComment->appGetCommentList($this->userId, 0, $page, $type);
        unset($data['total_score'],$data['service'],$data['total_comment'],$data['product'],$data['desc']);
        $this->appReturn(1, '', $data);
    }

    /**
     * 评论回复
     */
    public function reply(){
        $this->loadModel('MerchantCommentModel', 'mComment', 'merchant');

        //表单验证
        $vConf = array(
            array('id', '评论id', 'required|is_numeric'),
            array('content', '回复内容', 'required')
        );
        $this->formDataValidation($vConf);

        $id = (int)post('id');
        $content = trim(post('content'));

        //验证是否存在
        $info = $this->mComment->getInfo($id, 0, 0, $this->userId);
        if(!$info){
            $this->appReturn(0, '评论信息不存在');
        }

        //保存回复
        if(!($this->mComment->save(array('reply' => $content), $id))){
            $this->appReturn(0, '回复失败');
        }

        $this->appReturn(1, '回复成功');
    }

    /**
     * 店铺财务统计
     */
    public function statistics(){
        $this->loadModel('ConsumerOrderModel', 'mOrder', 'consumer');

        //营业额
        $data = $this->mOrder->merchantOrderStatistics($this->userId);

        //实际收款
        $tA = $this->mOrder->merchantOrderStatistics($this->userId, true);

        $data['month_amount'] = $data['month_amount']?$data['month_amount']:'0';
        $data['reality_amount'] = $tA['reality_amount']?$tA['reality_amount']:'0';
        $data['reality_month_amount'] = $tA['reality_month_amount']?$tA['reality_month_amount']:'0';

        $this->appReturn(1,'', $data);
    }

    /**
     * 财务统计明细
     */
    public function statisticsDetail(){
        $this->loadModel('ConsumerOrderModel', 'mOrder', 'consumer');
        //表单验证
        $vConf = array(
            array('month', '月份', 'required|is_numeric'),
            array('type', '类型', 'required|in_list[1,2]'),
            array('year', '年份', 'required|is_numeric'),
            array('page', '页码', 'required|is_natural_no_zero')
        );
        $this->formDataValidation($vConf);

        $month = post('month');
        $type = post('type');
        $year = post('year');
        $page = post('page');

        $list = $this->mOrder->appMerchantOrderStatisticsList($this->userId, $year, $month, $page, $type);

        $this->appReturn(1,'', $list);
    }

    /**
     * 消息管理
     */
    public function getMessageList(){
        $this->loadModel('MerchantMessageModel', 'mMessage', 'merchant');

        //表单验证
        $vConf = array(
            array('page', '页码', 'required|is_natural_no_zero'),
            array('type', '类型', 'required|in_list[1,3]'),
        );

        $this->formDataValidation($vConf);

        $page = (int)post('page');
        $type = (int)post('type');

        $data = $this->mMessage->appGetMessageList($this->userId, $type, $page);

        $this->appReturn(1, '', $data);
    }

    /**
     * 系统消息详情
     */
    public function messageDetail(){
        $this->loadModel('MerchantMessageModel', 'mMessage', 'merchant');

        //表单验证
        $vConf = array(
            array('id', '消息id', 'required|is_natural_no_zero'),
        );

        $this->formDataValidation($vConf);

        $id = (int)post('id');

        //验证消息是否存在
        $info = $this->mMessage->getInfo($id);
        if(!$info){
            $this->appReturn(0, '消息不存在');
        }

        $info['addtime'] = date('Y年m月d日 H:i', $info['addtime']);

        $tMsg = array(
            'message_id' => $id,
            'merchant_id' => $this->userId,
            'order_id' => $info['order_id'],
            'is_delete' => 1,
            'addtime' => time(),
        );
        //保存查看记录
        if($info['viewed'] == 1){
            $id = 0;
        }
        if(!($this->mMessage->saveView($tMsg, $id))){
            $this->appReturn(0, '保存失败');
        }

        $html = $this->view('message/detail',['html'=>$info],0,0,0,true);

        $this->ajaxReturn(1, '', $html);
//        $this->appReturn(1, '', $info);

    }

    /**
     * 删除消息
     */
    public function delMessage(){
        $this->loadModel('MerchantMessageModel', 'mMessage', 'merchant');

        //表单验证
        $vConf = array(
            array('id', '消息id', 'required|is_natural_no_zero'),
        );

        $this->formDataValidation($vConf);

        $id = (int)post('id');

        //验证消息是否存在
        $info = $this->mMessage->getInfo($id);
        if(!$info){
            $this->appReturn(0, '消息不存在');
        }

        if($info['viewed'] == 1){
            $this->appReturn(0, '未读消息不能删除');
        }
        if($info['type'] == 2){//指定人的系统消息
            //真删保存
            $query = $this->mMessage->deleteMessage($id, $this->userId);
            $tA = $this->mMessage->deleteView( $this->userId, $id);
            if (!$tA){
                $this->appReturn(0, '删除失败');
            }
        }else{
            //假删保存
            $query = $this->mMessage->saveView(array('is_delete' => 2), $id);
        }
//        if($info['type'] == 1){ //系统消息（所有人）
//            //假删保存
//            $query = $this->mMessage->saveView(array('is_delete' => 2), $id);
//        }else{
//            //真删保存
//            $query = $this->mMessage->deleteMessage($id, $this->userId);
//            $tA = $this->mMessage->deleteView($id, $this->userId);
//            if (!$tA){
//                $this->appReturn(0, '删除失败');
//            }
//        }

        if(!$query){
            $this->appReturn(0, '删除失败');
        }
        $this->appReturn(1, '删除成功');
    }

}